//引入pinia
import { defineStore } from "pinia";
import {
  apiGetUserInfo,
  apiGetMenuTree,
  apiGetUserDetail,
} from "@/api/common/api.js";
// import routerMenu from "@/hooks/processRoutingMenu.js"; //引入方法.
import router from "@/router/index";
import { ElMessage, ElNotification } from "element-plus";
export const useStore = new defineStore("leaStore", {
  state() {
    return {
      isCollapse: false,
      menuList: [],
      pathList: [], // 路由路径
      titleList: [],
      accountObjItem: {},
      tabsMenuList: [],
      token: "", //token
      redirectPath: "/adminHome", //重定向路径
      loginPath: "", //登录页面
	  paramsObject: {}, //参数对象  如果使用pinia 传参 参数字段名不要重复 否则可能影响其他数据
    };
  },
  actions: {
    // 返回早上好，晚上好等
    getTimeMsg() {
      const hour = new Date().getHours();
      let greeting = "";
      if (hour >= 0 && hour < 6) {
        greeting = "凌晨好";
      } else if (hour >= 6 && hour < 12) {
        greeting = "早上好";
      } else if (hour >= 12 && hour < 14) {
        greeting = "中午好";
      } else if (hour >= 14 && hour < 18) {
        greeting = "下午好";
      } else {
        greeting = "晚上好";
      }
      return greeting;
    },
    // 展开关闭
    openClose(bool) {
      this.isCollapse = bool;
    },
    // 菜单
    getMenuList(data) {
      // 获取菜单
		  let list = data;
		  list.forEach((item) => {
		          item.icon = item.iconCls || "";
		          item.path = item.url || "";
		          item.name = item.text || "";
		          if (item.children && item.children.length > 0) {
		            this.getMenuList(item.children);
		          }
		   });
		   return list;
    },

    // 设置tabs标签页
    setTabsMenuList(arr) {
      this.tabsMenuList = arr;
    },

    // 设置标题栏
    setTitleList(arr) {
      this.titleList = arr;
    },
    // 设置token
    setToken(token) {
      this.token = token;
    },

    // 获取用户信息
    getUserInfo(url) {
      apiGetUserInfo({ menu: true }, "加载中").then((res) => {
        if (res.success) {
              this.accountObjItem = res.data;
			  this.menuList = this.getMenuList(res.data.menus || []); //获取菜单
			  this.pathList = []; //获取菜单
			  console.log(this.pathList);
			  console.log(this.menuList);
		        this.redirectPath = url=='/login'?'userHome':'adminHome'; //路由重定向地址页面路径
		        this.loginPath = url; //路由重定向地址页面路径
		        router.replace("/");
		        setTimeout(() => {
		          ElNotification({
		            title: `登录成功`,
		            message: h(
		              "i",
		              { style: "color: teal" },
		              `${this.getTimeMsg()},${this.accountObjItem.userName || ""}`
		            ),
		            type: "success",
		            duration: 3 * 1000,
		          });
		        }, 500);
		        ElMessage.success("登录成功");
        }
      });
    },
    // 清空 pinia里面的数据
    clearStore() {
      this.isCollapse = false;
      this.menuList = [];
      this.titleList = [];
      this.accountObjItem = {};
      this.tabsMenuList = [];
      this.token = "";
      this.pathList = [];
    },
	// 设置参数
	setParams(obj) {
	  this.paramsObject = obj;
	},
  },
  persist: true, // 使用 persist 标记启用数据持久化
});
